﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CommunityServer.WebServices.MediaGalleries;
using System.Diagnostics;
using System.Web;
using System.Threading;

namespace nb.development.csmediamanager
{
    public partial class GalleryProperties : Form
    {
        public GalleryProperties(MediaGallery gallery, Guid SiteId)
        {
            InitializeComponent();
            this.siteid = SiteId;
            this.gallery = gallery;
            MenuItem i = new MenuItem("&Copy");
            i.DefaultItem = true;
            i.Click += new EventHandler(copy_Click);
            GalleryUrl.ContextMenu = new ContextMenu(new MenuItem[] { i });
            i = new MenuItem("&Copy");
            i.DefaultItem = true;
            i.Click += new EventHandler(copy2_Click);
            RSSUrl.ContextMenu = new ContextMenu(new MenuItem[] { i });
            init();
        }

        private void copy_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(gallery.Url);
        }

        private void copy2_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(gallery.Url);
        }

        private void init()
        {
            title.Text = HttpUtility.HtmlDecode(gallery.Name);
            this.Text = string.Format("{0} Properties - CS Media Manager", HttpUtility.HtmlDecode(gallery.Name));
            GalleryUrl.Text = gallery.Url;
            RSSUrl.Text = gallery.Rss;
            Key.Text = gallery.Key;
            created.Text = gallery.DateCreated.Value.ToLongDateString() + " " + gallery.DateCreated.Value.ToLongTimeString();
            description.Text = HttpUtility.HtmlDecode(gallery.Description);
            if (gallery.Settings.Enabled.HasValue)
                enabled.Checked = gallery.Settings.Enabled.Value;
            else enabled.Enabled = false;
            if (gallery.Settings.EnableCommentNotification.HasValue)
                EnableCommentNotification.Checked = gallery.Settings.EnableCommentNotification.Value;
            else EnableCommentNotification.Enabled = false;
            if (gallery.Settings.EnableComments.HasValue)
                EnableComments.Checked = gallery.Settings.EnableComments.Value;
            else EnableComments.Enabled = false;
            if (gallery.Settings.EnableDisclaimer.HasValue)
                EnableDisclaimer.Checked = gallery.Settings.EnableDisclaimer.Value;
            else EnableDisclaimer.Enabled = false;
            if (gallery.Settings.EnableExternalLinks.HasValue)
                EnableExternalLinks.Checked = gallery.Settings.EnableExternalLinks.Value;
            else EnableExternalLinks.Enabled = false;
            if (gallery.Settings.EnableMediaGalleryPostNotification.HasValue)
                EnableMediaGalleryPostNotification.Checked = gallery.Settings.EnableMediaGalleryPostNotification.Value;
            else EnableMediaGalleryPostNotification.Enabled = false;
            if (gallery.Settings.EnableRatings.HasValue)
                EnableRatings.Checked = gallery.Settings.EnableRatings.Value;
            else EnableRatings.Enabled = false;
            if (gallery.Settings.EnableRssSyndication.HasValue)
                EnableRssSyndication.Checked = gallery.Settings.EnableRssSyndication.Value;
            else EnableRssSyndication.Enabled = false;
            if (gallery.Settings.EnableTagsRss.HasValue)
                EnableTagsRss.Checked = gallery.Settings.EnableTagsRss.Value;
            else EnableTagsRss.Enabled = false;
            if (gallery.Settings.IsSearchable.HasValue)
                IsSearchable.Checked = gallery.Settings.IsSearchable.Value;
            else IsSearchable.Enabled = false;
            if (gallery.Settings.ModerateComments.HasValue)
                ModerateComments.Checked = gallery.Settings.ModerateComments.Value;
            else ModerateComments.Enabled = false;
            adEx.Text = gallery.Settings.AllowedFileExtensions;
            reEx.Text = gallery.Settings.RestrictedFileExtensions;
            MaxFileSize.Value = gallery.Settings.MediaGalleryFilesMaxSize.Value;
            Quota.Value = gallery.Settings.MediaGalleryFilesQuota.Value;
        }

        private MediaGallery gallery;
        private Guid siteid;
        public event Saved Saved;
        private void panel_Paint(object sender, PaintEventArgs e)
        {
            Panel p = sender as Panel;
            e.Graphics.DrawLine(new Pen(Color.FromArgb(160, 160, 160)), 1, 1, p.Width, 1);
        }

        private void GalleryUrl_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ProcessStartInfo sInfo = new ProcessStartInfo(((LinkLabel)sender).Text);
            try { Process.Start(sInfo); }
            catch { }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void save()
        {
            gallery.Name = HttpUtility.HtmlEncode(title.Text);
            gallery.Key = Key.Text;
            gallery.Description = HttpUtility.HtmlEncode(description.Text);
            if (gallery.Settings.Enabled.HasValue)
                gallery.Settings.Enabled = enabled.Checked;
            if (gallery.Settings.EnableCommentNotification.HasValue)
                gallery.Settings.EnableCommentNotification = EnableCommentNotification.Checked;
            if (gallery.Settings.EnableComments.HasValue)
                gallery.Settings.EnableComments = EnableComments.Checked;
            if (gallery.Settings.EnableDisclaimer.HasValue)
                gallery.Settings.EnableDisclaimer = EnableDisclaimer.Checked;
            if (gallery.Settings.EnableExternalLinks.HasValue)
                gallery.Settings.EnableExternalLinks = EnableExternalLinks.Checked;
            if (gallery.Settings.EnableMediaGalleryPostNotification.HasValue)
                gallery.Settings.EnableMediaGalleryPostNotification = EnableMediaGalleryPostNotification.Checked;
            if (gallery.Settings.EnableRatings.HasValue)
                gallery.Settings.EnableRatings = EnableRatings.Checked;
            if (gallery.Settings.EnableRssSyndication.HasValue)
                gallery.Settings.EnableRssSyndication = EnableRssSyndication.Checked;
            if (gallery.Settings.EnableTagsRss.HasValue)
                gallery.Settings.EnableTagsRss = EnableTagsRss.Checked;
            if (gallery.Settings.IsSearchable.HasValue)
                gallery.Settings.IsSearchable = IsSearchable.Checked;
            if (gallery.Settings.ModerateComments.HasValue)
                gallery.Settings.ModerateComments = ModerateComments.Checked;
            gallery.Settings.AllowedFileExtensions = adEx.Text;
            gallery.Settings.RestrictedFileExtensions = reEx.Text;
            gallery.Settings.MediaGalleryFilesMaxSize = long.Parse(MaxFileSize.Value.ToString());
            gallery.Settings.MediaGalleryFilesQuota = Convert.ToInt32(Quota.Value);
            this.Cursor = Cursors.WaitCursor;
            this.ControlBox = false;
            this.Enabled = false;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            save();
            GalleryWorker gw = new GalleryWorker(siteid, gallery);
            gw.UpdateCompleted += new GalleryWorkerUpdateCompleted(gw_UpdateCompleted);
            Thread t = new Thread(new ThreadStart(gw.Update));
            t.Start();
        }

        void gw_UpdateCompleted(Guid siteid, MediaGallery gallery)
        {
            if (this.InvokeRequired)
            {
                try
                {
                    GalleryWorkerUpdateCompleted done = new GalleryWorkerUpdateCompleted(gw_UpdateCompleted);
                    this.Invoke(done, siteid, gallery);
                }
                catch { }
            }
            else
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
                Saved(gallery, siteid);
            }
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            save();
            GalleryWorker gw1 = new GalleryWorker(siteid, gallery);
            gw1.UpdateCompleted += new GalleryWorkerUpdateCompleted(gw1_UpdateCompleted);
            Thread t = new Thread(new ThreadStart(gw1.Update));
            t.Start();
        }

        void gw1_UpdateCompleted(Guid siteid, MediaGallery gallery)
        {
            if (this.InvokeRequired)
            {
                try
                {
                    GalleryWorkerUpdateCompleted done = new GalleryWorkerUpdateCompleted(gw_UpdateCompleted);
                    this.Invoke(done, siteid, gallery);
                }
                catch { }
            }
            else
            {
                this.gallery = gallery; 
                init(); 
                this.Enabled = true; 
                this.Cursor = Cursors.Default;
                this.ControlBox = true;
                Saved(gallery, siteid); 
            }
        }
    }
    public delegate void Saved(MediaGallery gallery, Guid siteid);
}
